package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.JobDemandMapper;
import com.example.mapper.ResumeClassifyMapper;
import com.example.mapper.ResumeMapper;
import com.example.mapper.WorkExperienceMapper;
import com.example.pojo.JobDemand;
import com.example.pojo.Resume;
import com.example.pojo.ResumeClassify;
import com.example.pojo.WorkExperience;
import com.example.service.IResumeClassifyService;
import com.example.util.DateUtils;
import com.example.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import static java.lang.System.err;

/**
 * <p>
 * 简历信息分类表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-04-14
 */
@Service
public class ResumeClassifyServiceImpl extends ServiceImpl<ResumeClassifyMapper, ResumeClassify> implements IResumeClassifyService {

    @Autowired
    private ResumeClassifyMapper rcm;
    @Autowired
    private ResumeMapper rm;
    @Autowired
    private JobDemandMapper jdm;
    @Autowired
    private WorkExperienceMapper wem;


    @Override
    public Integer calJobMatch(Integer rid){
        Resume r = rm.selectById(rid);
        ResumeClassify rc = rcm.selectOne(new QueryWrapper<ResumeClassify>().eq("resume_id", rid));
        err.println("rc.getJobDemandId() -> " + rc.getJobDemandId());
        JobDemand jd = jdm.selectById(rc.getJobDemandId());
        List<WorkExperience> we = wem.selectList(new QueryWrapper<WorkExperience>().eq("resume_id", rid));

        // 匹配度
        int al = 0;
        // 专业
        if (jd.getMajor() == null){
            al = al + 25;
        }else if (jd.getMajor() != null){
            int i = jd.getMajor().indexOf(r.getMajor());
            if (i >= 0){
                al = al + 25;
            }
            System.err.println("判断专业 -》 "  + i);
        }
        // 学历
        if (Integer.parseInt(jd.getEducation()) == 0){
            al = al +25;
        }else if (Integer.valueOf(jd.getEducation()).equals(r.getHighestEducation())){
            al = al + 25;
        }
        // 工龄
        int los = 0;
        if ( we.size() > 0){
            for (WorkExperience e : we) {
                String a = e.getHireDate() + "";
                String b = e.getDepartureDate() + "";
                int i = DateUtils.yearDateDiff(a, b);
                los = los + i;
            }
        }
        System.err.println("工龄 -> " + los);

        int inetwe = Integer.parseInt(jd.getWorkExperience());
        if (inetwe == 1){
            al = al + 25;
        }else if (inetwe == 2){
            if (los >= 1){
                al = al + 25;
            }
        }else if (inetwe == 3){
            if (los >= 2){
                al = al + 25;
            }
        }else if (inetwe == 4){
            if (los >= 3){
                al = al + 25;
            }
        }else if (inetwe == 5){
            if (los >= 5){
                al = al + 25;
            }
        }else if (inetwe == 6){
            if (los >= 8){
                al = al + 25;
            }
        }else if (inetwe == 7){
            if (los >= 10){
                al = al + 25;
            }
        }

        // 年龄
        int byBirth = 0;
        if (r.getBirth() != null){
            try {
                byBirth = DateUtils.getAgeByBirth(r.getBirth());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            System.err.println("年龄  -- > " + byBirth);
            if (jd.getAgeMin() <= byBirth  && byBirth <= jd.getAgeMax()){
                al = al + 25;
            }
        }

        err.println(" al - > " + al);
        return al;
    }

    @Override
    public Integer insResc(ResumeClassify resumeClassify, Integer rsuId) {
        resumeClassify.setResumeId(rsuId);
        rcm.insert(resumeClassify);
        Integer mdegree = calJobMatch(rsuId);
        ResumeClassify classify = new ResumeClassify();
        classify.setId(resumeClassify.getId());
        classify.setMatchDegree(mdegree);
        return rcm.updateById(classify);
    }

    /**
     * @param rid 员工编号
     * @return
     */
    @Override
    public List<Map<String, Object>> selRescById(Integer rid) {
        List<Map<String, Object>> list = rcm.selById(rid);

        if (!list.isEmpty()){
            return list;
        }
        return null;
    }

    /**
     * 修改简历分类
     */
    @Override
    public Integer uptResc(ResumeClassify rc) {
        int i = rcm.update(rc, new UpdateWrapper<ResumeClassify>()
                .eq("resume_id", rc.getResumeId()));
        return i;
    }

    @Override
    public ResultVo getHireNotYetEntryResume() {
        List<Map<String, Object>> maps = rcm.selectHireNotYetEntryResumeClassify();
        return ResultVo.response(maps);
    }
}
